<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
	var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
        $mainframe = JFactory::getApplication();
        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('com_frtown.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
        $this->setState('limit', 5);
        $this->setState('limitstart', $limitstart);
    }
    
    /**
	*Get data table PREF add in pulldown
	*/
    function getPREF(){
        $this->_db->setQuery("SELECT PREF_CODE, PREF_NAME FROM pref");
        return $this->_db->loadObjectList();
    }
    
    /**
	*Get Total
	*/
    function getTotal($where = null){
        //Load the content if it doesn't already exist
        if($this->_total === null){
            $query = "SELECT COUNT(*) FROM town $where";
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        //echo $query;
        return $this->_total;
        
    }
    
    /**
	*Get Pagination
	*/
    function getPagination(){
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
    
    /**
	*Get data table TOWN add in list
	*/
    function getTown(){
        
        $keyword = JRequest::getVar('keyword');
        $pref = JRequest::getVar('pref');
        $valueStart = JRequest::getVar('TOWN_POPU_START');
        $valueEnd = JRequest::getVar('TOWN_POPU_END');
        $where = array();
        if($pref == 0){
            $where[] = " TOWN_NAME LIKE '%$keyword%' ";
        }
        else{
            $where[] = "TOWN_NAME LIKE '%$keyword%'";
            $where[]="TOWN_PREF_CODE = $pref ";
        }
        
        if($valueStart && $valueEnd){
            $where[]= "TOWN_POPU BETWEEN $valueStart AND $valueEnd ";
        }
        $where[]="TOWN_FLAG = 1";
        $where[]="TOWN_WEBF = 1";
        $sql = "SELECT TOWN_CODE,TOWN_NAME,TOWN_KANA,TOWN_POST,TOWN_TEL,TOWN_FAX,TOWN_ADD1,TOWN_ADD2,TOWN_URL,TOWN_HDATE,TOWN_HNAME,TOWN_DNAME,TOWN_EDATE,TOWN_ENAME
                FROM town ";
       if($where){
            $where = ' WHERE '.implode(' AND ', $where);
        }else{
            $where = null;
        }
       // get total number record
       $this->_total = $this->getTotal($where);
       $sql .= $where;
        //Order by
        $sql .= " ORDER BY TOWN_CODE DESC";
        
        //echo $sql;
        
        //Pagination
        $sql .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
        //echo $sql;
        //echo $sql;
        $this->_db->setQuery($sql);
        return $this->_db->loadObjectList();
    }
    
    /**
	*Load data TOWN for Edit
	*/
    function load($cid){
        $table = &JTable::getInstance('bg','bgTable');
        $table->load($cid);
        return $table;
    }
    
    /**
	*Load data CENTER/CLUB LIST
	*/
	function getCenterClub($id){
       $qr = "SELECT DISTINCT OCBA_CODE,OCBA_NAME,OCBA_KANA,CLUB_NAME,
       OCBA_POST,OCBA_ADD1,OCBA_ADD2,OCBA_TEL,OCBA_FAX,OCBA_CONT,
       OCBA_EFLAG,OCBA_LNAME,OCBA_LPOST,OCBA_LADD1,OCBA_LADD2,OCBA_LTEL,
       OCBA_LFAX,POOL_OCBA_CODE,POOL_POHK,GYMT_OCBA_CODE,DOCK_OCBA_CODE,DOCK_DOCM_CODE
       ,OCBA_PUTF,OCBA_LARF,OCBA_CANF,OCBA_YACF,OCBA_SCHF,OCBA_OUTF
                FROM ocba
                LEFT JOIN gymt ON OCBA_CODE = GYMT_OCBA_CODE
                LEFT JOIN pool ON OCBA_CODE = POOL_OCBA_CODE
                LEFT JOIN dock ON OCBA_CODE = DOCK_OCBA_CODE
                LEFT JOIN club ON OCBA_CLUB_CODE = CLUB_CODE
                WHERE OCBA_TOWN_CODE = '$id' 
                AND OCBA_WEBF = 1
                AND OCBA_FLAG = 1
                ";
       //echo $qr;
       $this->_db->setQuery($qr);
       $result= $this->_db->loadObjectList();
       return $result;
      
	}
    
    /**
	*Load data OCBA_NAME, OCBA_CODE
	*/
	function getOCBA($id){
       $qr = "SELECT OCBA_CODE,OCBA_NAME,OCBA_KANA
                FROM ocba
                WHERE OCBA_TOWN_CODE = '$id' 
                AND OCBA_WEBF = 1
                AND OCBA_FLAG = 1
                ";
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       return $result;
	}
}
